package redis

import (
	"context"
	"fmt"
	"go-toy/util"

	"github.com/redis/go-redis/v9"
)

var Ctx = context.Background()

var Client *redis.Client

func init() {
	redisConfigMap := util.Viper.GetStringMap("redis")
	if len(redisConfigMap) != 0 {

		var addr string
		var password string

		host := util.Viper.GetString("redis.host")
		port := util.Viper.GetString("redis.port")
		addr = fmt.Sprintf("%s:%s", host, port)
		password = util.Viper.GetString("redis.password")
		db := util.Viper.GetInt("redis.db")

		Client = redis.NewClient(&redis.Options{
			Addr:     addr,
			Password: password,
			DB:       db,
		})
	}
}
